//package com.example.mongo.appmongo.utils;
//
//import com.example.mongo.appmongo.config.UserSimpleMongoClientDatabaseFactory;
//import com.mongodb.ConnectionString;
//import com.mongodb.client.MongoClient;
//import com.mongodb.client.MongoClients;
//import org.springframework.beans.factory.NoSuchBeanDefinitionException;
//import org.springframework.beans.factory.support.BeanDefinitionBuilder;
//import org.springframework.beans.factory.support.DefaultListableBeanFactory;
//import org.springframework.context.ApplicationContext;
//import org.springframework.context.ConfigurableApplicationContext;
//import org.springframework.data.mongodb.MongoTransactionManager;
//import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
//import org.springframework.stereotype.Service;
//
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * @author sjw
// * @className TenantContext
// * @date 2021/9/29 15:52
// * @description: TODO
// */
//@Service
//public class TenantContext {
//    private static String defaultTm="transactionManager";
//    private static ThreadLocal<String> tenantContext=new ThreadLocal<>();
//    private static Map<String, MongoClient> mongoClientMap=new HashMap<>();
//    private static Map<String, SimpleMongoClientDatabaseFactory> mongoClientDatabaseFactoryMap=new HashMap<>();
//    public static void setTenant(String tenant){
//
//        registerTxManager();
//
//        tenantContext.set(tenant);
//    }
//    public static ApplicationContext applicationContext;
//
//    public static String getTenant(){
//        String tenant=tenantContext.get();
//        if(tenant==null)tenant=defaultTm;
//        System.out.println(tenant);
//
//        return tenant;
//    }
//
//    public static MongoClient getMongoClient(){
//        System.out.println("getMongoClient---------");
//        return mongoClientMap.get(getTenant());
//    }
//    public static void putMongoClient(String tenant, MongoClient mongoClient){
//        mongoClientMap.put(tenant,mongoClient);
//    }
//
//
////    public static SimpleMongoClientDatabaseFactory getDatabaseFactory(){
////        System.out.println("getDatabaseFactory---------");
////        return mongoClientDatabaseFactoryMap.get(getTenant());
////    }
////    public static void putDatabaseFactory(String tenant, SimpleMongoClientDatabaseFactory mongoClientDatabaseFactory){
////        mongoClientDatabaseFactoryMap.put(tenant,mongoClientDatabaseFactory);
////    }
//
//
//    public static void registerTxManager(){
//        Object o=null;
//        try {
//            o =applicationContext.getBean("transactionManager1");
//        }
//        catch (NoSuchBeanDefinitionException ex){
//
//        }
//        if(o!=null) return;
//        ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) applicationContext;
//        //获取BeanFactory
//        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableApplicationContext.getBeanFactory();
//        //创建bean信息.
//        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(MongoTransactionManager.class);
//        ConnectionString connectionString= new ConnectionString("mongodb://dbadmin:qj123456789@192.168.10.127:27017,192.168.10.127:27018,192.168.10.127:27019/daily_report?repliaSet=rs0");
//        MongoClient client= MongoClients.create(connectionString);
//        TenantContext.putMongoClient("transactionManager1",client);
//        //UserSimpleMongoClientDatabaseFactory factory= new UserSimpleMongoClientDatabaseFactory(client,connectionString.getDatabase());
//        //TenantContext.putDatabaseFactory("transactionManager1",factory);
//
////        beanDefinitionBuilder.addPropertyValue("dbFactory", factory);
//        defaultListableBeanFactory.registerBeanDefinition("transactionManager1", beanDefinitionBuilder.getBeanDefinition());
////        ((ConfigurableApplicationContext) applicationContext).refresh();
//    }
//
//}
